package com.xuwenjin.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestNewInstance {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class<?> aClass = Class.forName("com.xuwenjin.reflection.User");
        //本质上是调用了类的无参构造,如果没有无参构造,就报错
        //并且类的构造器的访问权限要足够
        User o = (User)aClass.newInstance();

        //invoke方法
        Method setName = aClass.getDeclaredMethod("setName",String.class);
        setName.invoke(o, "haha");


        //关闭安全检查开关,私有的属性和方法需要执行该操作,还能提高反射的效率,执行的时间短
//        setName.setAccessible(true);


    }
}
